<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2020/7/2
 * Time: 14:03
 */

namespace app\api\controller;


use think\Db;

class Match extends Base
{
    private $user_id; // 用户ID
    private $page; // 页码
    private $create_time;
    
    /**
     * 异地登录验证
     * @return string
     */
    public function __construct()
    {
        $this->user_id = input('user_id');
        $this->page = input('page') ? input('page') : 1;
        $this->create_time = date('Y-m-d H:i:s');
        $time = input('time');
        $value = input('value');
        $reqKey = input('reqKey');
        $auth = $this->checkAll($this->user_id,$time,$value,$reqKey);
        if($auth == 112){
            echo json_encode(['status'=>301,'msg'=>$this->otherLogin]);
            exit;
        }elseif($auth == 110){
            echo json_encode(['status' => 302, 'msg' => $this->frost]);
            exit;
        }elseif($auth == 2111){
            echo json_encode(['status' => 303, 'msg' => $this->del]);
            exit;
        }
    }
    
    /**
     * 赛事列表
     * @return \think\response\Json
     * match_cate      赛事类型    0 全部  1 帆船  2 赛艇
     * match_status    赛事状态    0 全部  1 未开始   2 进行中   3 已结束
     * match_time      选择的时间  2020-07-02
     * keyword         关键字
     * page            页码
     */
    public function matchList(){
        $match_cate = input('match_cate'); // 0 全部   1 帆船   2 赛艇
        $match_status = input('match_status'); // 0 全部 1 未开始   2 进行中   3 已结束
        $match_time = input('match_time'); // 开始时间 2020-07-02
        $keyword = input('keyword'); // 关键字
        $where = [];
        if(isset($match_cate) && $match_cate != 0){
            $where['match_cate'] = $match_cate;
        }
        if(isset($match_status) && $match_status != 0){
            $where['match_status'] = $match_status;
        }
        if(isset($keyword) && $keyword != ''){
            $where['match_title'] = ['like', '%' . $keyword . '%'];
        }
        if(isset($match_time) && $match_time != ''){
            $begin_time = date('Y-m-d 00:00:00', strtotime($match_time));
            $end_time = date('Y-m-d 23:59:59', strtotime($match_time));
            $where['match_start_time'] = ['between', [$begin_time, $end_time]];
        }
        $list = Db::table('up_match')
            ->join('up_area', 'district_id = match_shi', 'left')
            ->where($where)
            ->field('match_id, match_title, match_image, district, match_country_name, match_cate, match_status')
            ->page($this->page, 10)
            ->order('match_create_time desc')
            ->select();
        if($list){
            return json(['status' => 200, 'msg' => $list]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 我收藏的赛事
     * @return \think\response\Json
     * match_cate      赛事类型    0 全部  1 帆船  2 赛艇
     * page            页码
     */
    public function myCollectMatch(){
        $match_cate = input('match_cate'); // 0 全部   1 帆船   2 赛艇
        $where['mm_user_id'] = $this->user_id;
        $where['match_status'] = ['NEQ', 4]; // 未取消的
        if(isset($match_cate) && $match_cate != 0){
            $where['match_cate'] = $match_cate;
        }
        
        $list = Db::table('up_my_match')
            ->join('up_match', 'mm_match_id = match_id', 'left')
            ->join('up_area', 'district_id = match_shi', 'left')
            ->where($where)
            ->field('match_id, match_title, match_image, district, match_country_name, match_cate, match_status')
            ->page($this->page, 10)
            ->order('match_create_time desc')
            ->select();
        if($list){
            return json(['status' => 200, 'msg' => $list]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 热门搜索
     * @return \think\response\Json
     */
    public function hotSearch(){
        $hot = Db::table('up_hot')
            ->field('hot_id, hot_word')
            ->select();
        if($hot){
            return json(['status' => 200, 'msg' => $hot]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 赛事详情
     * @return \think\response\Json
     * match_id    赛事ID
     */
    public function matchDetail(){
        $match_id = input('match_id');
        $detail = Db::table('up_match')
            ->field('match_id, match_title, match_image, match_start_time, match_addr, 
            match_lon, match_lat, match_committee_phone, match_cate,match_status')
            ->where('match_id', $match_id)
            ->find();
        $collect = Db::table('up_my_match')
            ->where('mm_user_id', $this->user_id)
            ->where('mm_match_id', $match_id)
            ->find();
        if($collect){
            $detail['collect'] = 1; // 已收藏
        }else{
            $detail['collect'] = 2; // 未收藏
        }
        // 是否已报名
        $apply = Db::table('up_match_apply_info')
            ->where('ma_user_id', $this->user_id)
            ->where('ma_match_id', $match_id)
            ->field('ma_apply_status, ma_baodao_status')
            ->find();
        if($apply){
            $detail['apply'] = $apply['ma_apply_status']; // 已报名
            $detail['baodao'] = $apply['ma_baodao_status']; // 报到状态
        }else{
            $detail['apply'] = 0; // 未报名
            $detail['baodao'] = 0;
        }
        // 是否已报到
        
        // 时间
        $detail['match_start_time'] = date('Y年m月d日', strtotime($detail['match_start_time']));
        // 未读的官方公告
        $detail['unread_num'] = $this->gf(2, $match_id);
        return json(['status' => 200, 'msg' => $detail]);
    }
    
    /**
     * 联系组委会
     * @return \think\response\Json
     * match_id    赛事ID
     */
    public function committeeCall(){
        $match_id = input('match_id');
        $call = Db::table('up_match')
            ->where('match_id', $match_id)
            ->value('match_committee_phone');
        return json(['status' => 200, 'msg' => $call]);
    }
    
    /**
     * 生活信息
     * @return \think\response\Json
     * match_id     赛事ID
     * ml_type      信息类型   1 班车信息   2 酒店信息   3 其他信息
     */
    public function life(){
        $match_id = input('match_id');
        $ml_type = input('ml_type'); // 1 班车信息   2 酒店信息  3 其他信息
        $life = Db::table('up_match_life')
            ->where('ml_type', $ml_type)
            ->where('ml_match_id', $match_id)
            ->field('ml_id, ml_content')
            ->find();
        if($life){
            $files = Db::table('up_match_life_info')
                ->where('mli_life_id', $life['ml_id'])
                ->field('mli_id, mli_life_id, mli_name, mli_url, mli_dis')
                ->select();
            if($files){
                $life['files'] = $files;
            }else{
                $life['files'] = [];
            }
            return json(['status' => 200, 'msg' => $life]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 官方公告
     * @return \think\response\Json
     * match_id     赛事ID
     * page         页码
     */
    public function guanFang(){
        $match_id = input('match_id');
        if(isset($match_id)){ // 赛事进入
            $gf = $this->gf(1, $match_id);
        }else{ // 消息进入
            $gf = $this->gf(1);
        }
        
        if($gf){
            // 已读/未读
            foreach($gf as $k => $v){
                if(strpos($v['mgf_read_user'], ',' . $this->user_id . ',') !== false){
                    $gf[$k]['is_read'] = 1; // 已读
                }else{
                    $gf[$k]['is_read'] = 2; // 未读
                }
            }
            return json(['status' => 200, 'msg' => $gf]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 官方公告详情
     * @return \think\response\Json
     * mgl_id    官方公告ID
     */
    public function gfDetail(){
        $mgl_id = input('mgl_id');
        $detail = Db::table('up_match_gf')
            ->where('mgf_id', $mgl_id)
            ->field('mgf_title, mgf_intro, mgf_read_user, mgf_time')
            ->find();
        $files = Db::table('up_match_gf_files')
            ->where('mgfff_gf_id', $mgl_id)
            ->field('mgff_id, mgff_file_url, mgff_file_name, mgff_file_bmp')
            ->select();
        if($files){
            $detail['files'] = $files;
        }else{
            $detail['files'] = [];
        }
        if(strpos($detail['mgf_read_user'], ',' . $this->user_id . ',') === false){
            $read_user = $detail['mgf_read_user'] . $this->user_id . ',';
            Db::table('up_match_gf')->where('mgf_id', $mgl_id)->setField('mgf_read_user', $read_user);
        }
        return json(['status' => 200, 'msg' => $detail]);
    }
    
    /**
     * 临时通知
     * @return \think\response\Json
     * match_id     赛事ID
     * page         页码
     */
    public function linShi(){
        $match_id = input('match_id');
        if(isset($match_id)){ // 赛事进入
            $ls = $this->ls(1, $match_id);
        }else{ // 消息进入
            $ls = $this->ls(1);
        }
        
        if($ls){
            // 已读/未读
            foreach($ls as $k => $v){
                if(strpos($v['mfls_read_user'], ',' . $this->user_id . ',') !== false){
                    $ls[$k]['is_read'] = 1; // 已读
                }else{
                    $ls[$k]['is_read'] = 2; // 未读
                }
            }
            return json(['status' => 200, 'msg' => $ls]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 临时通知详情
     * @return \think\response\Json
     * mfls_id    临时通知ID
     */
    public function lsDetail(){
        $mfls_id = input('mfls_id');
        $detail = Db::table('up_match_ls')
            ->where('mfls_id', $mfls_id)
            ->field('mfls_title, mfls_match_intro, mfls_read_user, mfls_time')
            ->find();
        $files = Db::table('up_match_ls_files')
            ->where('mll_ls_id', $mfls_id)
            ->field('mll_id, mll_file_url, mll_file_name, mll_file_bmp')
            ->select();
        if($files){
            $detail['files'] = $files;
        }else{
            $detail['files'] = [];
        }
        if(strpos($detail['mfls_read_user'], ',' . $this->user_id . ',') === false){
            $read_user = $detail['mfls_read_user'] . $this->user_id . ',';
            Db::table('up_match_ls')->where('mfls_id', $mfls_id)->setField('mfls_read_user', $read_user);
        }
        return json(['status' => 200, 'msg' => $detail]);
    }
    
    /**
     * 赛事图片
     * @return \think\response\Json
     * match_id   赛事ID
     * page       页码
     */
    public function matchImage(){
        $match_id = input('match_id');
        $images = Db::table('up_match_images')
            ->where('mi_match_id', $match_id)
            ->field('mi_id, mi_match_id, mi_url, mi_name, mi_paishe_name, mi_paishe_time, mi_price, mi_create_time')
            ->page($this->page, 10)
            ->order('mi_create_time desc')
            ->select();
        // 是否已加入订单或已购买
        if($images){
            $image_id = Db::table('up_order')
                ->where('order_cate', 'IN', '1,2')
                ->where('order_user_id', $this->user_id)
                ->where('order_status', 'IN', '1,2')
                ->column('order_image_id');
            $img_ids_str = implode(',', $image_id);
            $img_ids_arr = explode(',', $img_ids_str);
            foreach($images as $k => $v){
                // 是否在订单中
                if($image_id){
                    if(in_array($v['mi_id'], $img_ids_arr)){
                        $images[$k]['is_pay'] = 1; // 有买过或下过订单
                    }else{
                        $images[$k]['is_pay'] = 2;
                    }
                }else{
                    $images[$k]['is_pay'] = 2;
                }
                // 是否已付钱
                $pay_money = Db::table('up_my_pay_images')
                    ->where('mpi_user_id', $this->user_id)
                    ->where('mpi_image_id', $v['mi_id'])
                    ->find();
                if($pay_money){
                    $images[$k]['pay_money'] = 1; // 已付款
                }else{
                    $images[$k]['pay_money'] = 2;
                }
            }
            return json(['status' => 200, 'msg' => $images]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 赛事视频
     * @return \think\response\Json
     * match_id   赛事ID
     * page       页码
     */
    public function matchVideo(){
        $match_id = input('match_id');
        $videos = Db::table('up_match_video')
            ->where('mv_match_id', $match_id)
            ->field('mv_id, mv_match_id, mv_video_url, mv_video_size, mv_video_name, mv_time')
            ->page($this->page, 10)
            ->order('mv_time desc')
            ->select();
        if($videos){
            return json(['status' => 200, 'msg' => $videos]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 帆船报名者是否实名认证
     * @return \think\response\Json
     * match_id   赛事ID
     */
    public function isShiMing(){
        $match_id = input('match_id');
        // 报名时间
        $time = strtotime(date('Y-m-d H:i'));
        $match = Db::table('up_match')
            ->where('match_id', $match_id)
            ->field('match_apply_start_time, match_apply_end_time, match_start_time')
            ->find();
        $begin = date('Y-m-d H:i', strtotime($match['match_apply_start_time']));
        $end = date('Y-m-d H:i', strtotime($match['match_apply_end_time']));
        $begin_time = strtotime($begin);
        $end_time = strtotime($end);
        if($time < $begin_time) return json(['status' => 0, 'msg' => $begin . '开始报名']);
        if($time > $end_time) return json(['status' => 0, 'msg' => '当前比赛已结束报名']);
        // 是否已经参加了其他赛事
        $otherMatch = Db::table('up_match_apply_info')
            ->join('up_match', 'match_id = ma_match_id', 'left')
            ->where('ma_user_id', $this->user_id)
//            ->where('match_end_time', '>', $match['match_start_time'])
            ->where('match_status', 'IN', '1,2')
            ->where('match_id', 'NEQ', $match_id)
            ->where('ma_apply_status', 2)
            ->field('match_id')
            ->find();
        if($otherMatch) return json(['status' => 0, 'msg' => '该赛事与您报名的其他赛事有冲突']);
        
        $shi = Db::table('up_u_user')->where('user_id', $this->user_id)->value('user_real_check');
        return json(['status' => 200, 'msg' => $shi]);
    }
    
    /**
     * 报名者的个人信息
     * @return \think\response\Json
     */
    public function applyerInfo(){
        $info = Db::table('up_u_user')
            ->field('user_real_name, user_gender, user_birthday, user_ID_number, user_email, user_call')
            ->where('user_id', $this->user_id)
            ->find();
        return json(['status' => 200, 'msg' => $info]);
    }
    
    /**
     * 赛事组别
     * @return \think\response\Json
     * match_id    赛事ID
     */
    public function matchGroup(){
        $match_id = input('match_id');
        $group = Db::table('up_match_apply_group')
            ->join('up_match_group', 'mg_id = mag_group_id', 'left')
            ->where('mag_match_id', $match_id)
            ->field('mag_id, mag_group_id, mag_apply_num, mag_already_num, mg_name')
            ->select();
        $lunci = Db::table('up_match')
            ->where('match_id', $match_id)
            ->field('match_lunci')
            ->find();
        return json(['status' => 200, 'msg' => $group, 'lunci' => $lunci]);
    }
    
    /**
     * 赛事轮次=====北京同事代码
     * @return \think\response\Json
     * match_id    赛事ID
     */
    public function matchTurn(){
        $match_id = input('match_id');
        $group_id = input('level_id');
        if(!$match_id||!$group_id){
            return json(['status' => 200, 'msg' => '参数不能为空']);
        }
        $data['disabled'] = Db::table('up__match_detail')
            ->where('match_id', $match_id)
            ->where('level_id', $group_id)
            ->where('flag', 3)
            ->order('turn')
            ->column('turn');
        $data['all'] = Db::table('up_match')
            ->where('match_id', $match_id)
            ->value('match_lunci');
        $data['all'] = range(1, $data['all']);
        return json(['status' => 200, 'msg' => 'success', 'data' => $data]);
    }
    
    /**
     * 队员个人信息
     * @return \think\response\Json
     * user_phone   用户手机号
     */
    public function joinerInfo(){
        $user_phone = input('user_phone');
        $user = Db::table('up_u_user')
            ->where('user_phone', $user_phone)
            ->field('user_id, user_real_name, user_gender, user_birthday, user_ID_number, user_email, 
            user_call, user_urg_phone, user_sailing_time, user_real_check, user_fanlian_ID, user_fanxie_ID')
            ->find();
        if($user){
            if($user['user_real_check'] == 1){
                return json(['status' => 0, 'msg' => '该用户未实名认证']);
            }
            return json(['status' => 200, 'msg' => $user]);
        }else{
            return json(['status' => 0, 'msg' => '用户不存在']);
        }
    }
    
    /**
     * 赛事冲突---报名添加船员
     * @return \think\response\Json
     * match_id       赛事ID
     * user_phone     用户手机号
     */
    public function isJoinOtherMatch(){
        $match_id = input('match_id');
        $user_phone = input('user_phone');
        $user = Db::table('up_u_user')
            ->where('user_phone', $user_phone)
            ->field('user_id')
            ->find();
        $match = Db::table('up_match')
            ->where('match_id', $match_id)
            ->field('match_start_time, match_cate')
            ->find();
        if(!$user) return json(['status' => 0, 'msg' => '用户不存在']);
        // 帆船赛事默认报名者参加比赛
        if($match['match_cate'] == 1){
            if($this->user_id == $user['user_id']) return json(['status' => 0, 'msg' => '不可以添加自己']);
        }
        // 是否已经参加过本场比赛
        $thisMatch = Db::table('up_match_apply_info')
            ->where('ma_user_id', $user['user_id'])
            ->where('ma_match_id', $match_id)
            ->where('ma_apply_status', 2)
            ->find();
        if($thisMatch) return json(['status' => 0, 'msg' => '该用户已经报名过这场比赛']);
        // 是否已经参加了其他赛事
        $otherMatch = Db::table('up_match_apply_info')
            ->join('up_match', 'match_id = ma_match_id', 'left')
            ->where('ma_user_id', $user['user_id'])
//            ->where('match_end_time', '>', $match['match_start_time'])
            ->where('match_status', 'IN', '1,2')
            ->where('match_id', 'NEQ', $match_id)
            ->where('ma_apply_status', 2)
            ->field('match_id')
            ->find();
        if($otherMatch) return json(['status' => 0, 'msg' => '该赛事与该队员报名的其他赛事有冲突']);
        return json(['status' => 200, 'msg' => $user['user_id']]);
    }
    
    /**
     * 赛事费用
     * @return \think\response\Json
     * match_id   赛事ID
     */
    public function cost(){
        $match_id = input('match_id');
        
        $cost_info = Db::table('up_match_cost_info')
            ->join('up_match_cost', 'mc_id = mi_cost_id', 'left')
            ->where('mi_match_id', $match_id)
            ->where('mi_del', 1)
            ->field('mi_id, mc_name, mi_price, mi_must')
            ->order('mi_must desc')
            ->select();
        $commi_call = Db::table('up_match')->where('match_id', $match_id)->value('match_committee_phone');
        return json(['status' => 200, 'msg' => $cost_info, 'commi_call' => $commi_call]);
    }
    
    /**
     * 提交报名----帆船
     * @return \think\response\Json
     */
    public function commitApply(){
        //************************************ 赛事
        $params = input('apply');
        $params = json_decode($params, true);
        
        $match_id = $params['match_id']; // 赛事ID
        $ma_total_price = $params['ma_total_price']; // 总费用
        $ma_cost_info_id = trim($params['ma_cost_info_id'], ','); // 费用明细ID组成的字符串 逗号隔开
        $ma_club_id = $params['ma_club_id']; // 俱乐部ID
        $ma_lingd = $params['ma_lingd']; // 领队
        $ma_coach = $params['ma_coach']; // 教练
        $ma_group = $params['ma_group']; // 组别ID   取mag_group_id
        $ma_fan_code = $params['ma_fan_code']; // 帆号
        $ma_boat_code = $params['ma_boat_code']; // 船号
        $sailor = $params['sailor']; // 船员
        //**********************************订单
        $order_type = $params['order_type']; // 订单类型   1 微信支付   2 支付宝支付   3 微信代付   4 支付宝代付
        
        $have_order = Db::table('up_order')
            ->where('order_user_id', $this->user_id)
            ->where('order_match_id', $match_id)
            ->find();
        if(!$have_order){
            $user = Db::table('up_u_user')
                ->where('user_id', $this->user_id)
                ->field('user_real_name, user_urg_phone, user_gender, user_birthday, user_ID_number, 
            user_email, user_call, user_sailing_time, user_username, user_phone, user_fanlian_ID, 
            user_fanxie_ID, user_weight, user_height')
                ->find();
    
            $match = Db::table('up_match')
                ->where('match_id', $match_id)
                ->field('match_cate, match_start_time, match_apply_end_time')
                ->find();
            
            // 比赛组别限制
            $group = Db::table('up_match_group')
                ->where('mg_id', $ma_group)
                ->field('mg_gender, mg_year, mg_weight, mg_height, mg_boat_year')
                ->find();
    
            //  ******  报名本人组别限制判断 start ******  //
            /*
            // 体重
            if(!$user['user_weight']){
                return json(['status' => 0, 'msg' => '请完善本人体重信息']);
            }else{
                if($user['user_weight'] > $group['mg_weight']) return json(['status' => 0, 'msg' => '本人体重超过体重限制']);
            }
            // 身高
            if(!$user['user_height']){
                return json(['status' => 0, 'msg' => '请完善本人身高信息']);
            }else{
                if($user['user_height'] > $group['mg_height']) return json(['status' => 0, 'msg' => '本人身高超过身高限制']);
            }
            // 性别
            if($group['mg_gender']){
                if($group['mg_gender'] != 3){
                    if($group['mg_gender'] != $user['user_gender']) return json(['status' => 0, 'msg' => '本人性别不符']);
                }
            }
            // 年龄
            if($group['mg_year']){
                $user_age = $this->getAge($user['user_birthday']);
                if($user_age > $group['mg_year']) return json(['status' => 0, 'msg' => '本人年龄超过限制年龄']);
            }
            // 船龄
            if($group['mg_boat_year']){
                $user_boat_age = $this->getAge($user['user_sailing_time']);
                if($user_boat_age > $group['mg_boat_year']) return json(['status' => 0, 'msg' => '本人船龄超过船龄限制']);
            }
            */
            //  ******  报名本人组别限制判断 end ******  //
            
            if(time() > strtotime($match['match_apply_end_time'])){
                return json(['status' => 0, 'msg' => '报名已结束']);
            }
    
            // 判断该赛事，该组别报名人数
            $apply_info = Db::table('up_match_apply_group')
                ->where('mag_match_id', $match_id)
                ->where('mag_group_id', $ma_group)
                ->find();
            if($apply_info['mag_apply_num'] <= $apply_info['mag_already_num']){
                return json(['status' => 0, 'msg' => '该赛事组别报名人数已满']);
            }
            // 帆号是否重复
            $fan = Db::table('up_match_apply_info')
                ->where('ma_match_id', $match_id)
                ->where('ma_fan_code', $ma_fan_code)
                ->find();
            if($fan) return json(['status' => 0, 'msg' => '帆号已被使用']);
            // 船号是否重复
            $chuan = Db::table('up_match_apply_info')
                ->where('ma_match_id', $match_id)
                ->where('ma_boat_code', $ma_boat_code)
                ->find();
            if($chuan) return json(['status' => 0, 'msg' => '船号已被使用']);
    
            $order_num = date('YmdHis') . str_pad(mt_rand(1, 9999), 4, '0', STR_PAD_LEFT);
    
            // 报名人信息
            $apply = [
                'ma_user_id' => $this->user_id,
                'ma_match_id' => $match_id,
                'ma_total_price' => $ma_total_price,
                'ma_cost_info_id' => $ma_cost_info_id,
                'ma_club_id' => $ma_club_id,
                'ma_lingd' => $ma_lingd,
                'ma_coach' => $ma_coach,
                'ma_group' => $ma_group,
                'ma_fan_code' => $ma_fan_code,
                'ma_boat_code' => $ma_boat_code,
                'ma_role' => 1,
                'ma_time' => $this->create_time,
                'ma_date' => date('Y-m-d'),
                'ma_match_cate' => $match['match_cate'],
                'ma_baodao_image' => $this->getQrcode($this->user_id, $match_id, 1),
                'ma_user_name' => $user['user_real_name'],
                'ma_user_gender' => $user['user_gender'],
                'ma_user_birthday' => $user['user_birthday'],
                'ma_user_ID_number' => $user['user_ID_number'],
                'ma_user_call' => $user['user_call'],
                'ma_user_email' => $user['user_email'],
                'ma_user_urg_phone' => $user['user_urg_phone'],
                'ma_user_sailing_time' => $user['user_sailing_time'],
                'ma_real_pay_user_id' => $this->user_id,
                'ma_order_num' => $order_num,
                'ma_user_fanxie_ID' => $user['user_fanxie_ID'],
                'ma_user_fanlian_ID' => $user['user_fanlian_ID']
            ];
    
            // 船员
            $chuany = [];
            $have = ['apply' => 1, 'user_name' => '']; // 用于判断某个用户有没有冲突的赛事
            $have_this = ['apply' => 1, 'user_name' => '']; // 用于判断有没有已经参加该赛事
            /*
            $_weight = ['apply' => 1, 'user_name' => '']; // 用于判断运动员体重限制
            $_height = ['apply' => 1, 'user_name' => '']; // 用于判断运动员身高限制
            $_gender = ['apply' => 1, 'user_name' => '']; // 用于判断运动员性别限制
            $_age = ['apply' => 1, 'user_name' => '']; // 用于判断运动员年龄限制
            $_boat_age = ['apply' => 1, 'user_name' => '']; // 用于判断运动员船龄限制
            */
            if(is_array($sailor) && !empty($sailor)){
                foreach($sailor as $k => $v){
                    $chuany[$k] = [
                        'ma_user_id' => $v['sailor_user_id'],
                        'ma_match_id' => $match_id,
                        'ma_total_price' => $ma_total_price,
                        'ma_cost_info_id' => $ma_cost_info_id,
                        'ma_club_id' => $ma_club_id,
                        'ma_lingd' => $ma_lingd,
                        'ma_coach' => $ma_coach,
                        'ma_group' => $ma_group,
                        'ma_fan_code' => $ma_fan_code,
                        'ma_boat_code' => $ma_boat_code,
                        'ma_role' => 2,
                        'ma_time' => $this->create_time,
                        'ma_date' => date('Y-m-d'),
                        'ma_match_cate' => $match['match_cate'],
                        'ma_user_name' => $v['sailor_user_name'],
                        'ma_user_gender' => $v['sailor_user_gender'],
                        'ma_user_birthday' => $v['sailor_user_birthday'],
                        'ma_user_ID_number' => $v['sailor_user_ID_number'],
                        'ma_user_call' => $v['sailor_user_call'],
                        'ma_user_email' => $v['sailor_user_email'],
                        'ma_user_urg_phone' => $v['sailor_user_urg_phone'],
                        'ma_user_sailing_time' => $v['sailor_user_sailing_time'],
                        'ma_real_pay_user_id' => $this->user_id,
                        'ma_order_num' => $order_num
                    ];
                    if(isset($v['sailor_user_fanlian_ID'])){
                        $chuany[$k]['ma_user_fanlian_ID'] = $v['sailor_user_fanlian_ID'];
                    }
                    if(isset($v['sailor_user_fanxie_ID'])){
                        $chuany[$k]['ma_user_fanxie_ID'] = $v['sailor_user_fanxie_ID'];
                    }
                    // 其他比赛
                    $otherMatch = Db::table('up_match_apply_info')
                        ->join('up_match', 'match_id = ma_match_id', 'left')
                        ->where('ma_user_id', $v['sailor_user_id'])
//                    ->where('match_end_time', '>', $match['match_start_time'])
                        ->where('match_status', 'IN', '1,2')
                        ->where('match_id', 'NEQ', $match_id)
                        ->field('match_id')
                        ->find();
                    if($otherMatch){
                        $have['apply'] = 2;
                        $have['user_name'] = $v['sailor_user_name'];
                        break;
                    }
                    // 本场比赛
                    $thisMatch = Db::table('up_match_apply_info')
                        ->join('up_match', 'match_id = ma_match_id', 'left')
                        ->where('ma_user_id', $v['sailor_user_id'])
                        ->where('match_id', $match_id)
                        ->field('match_id')
                        ->find();
                    if($thisMatch){
                        $have_this['apply'] = 2;
                        $have_this['user_name'] = $v['sailor_user_name'];
                        break;
                    }
                    
                    /*
                    $_user = Db::table('up_u_user')
                        ->where('user_id', $v['sailor_user_id'])
                        ->field('user_sailing_time, user_weight, user_height, user_gender, user_birthday, 
                        user_real_name')
                        ->find();
                    // 体重
                    if(!$_user['user_weight']){
                        $_weight['apply'] = 3; // 需要完善体重信息
                        $_weight['user_name'] = $_user['user_real_name'];
                        break;
                    }else{
                        if($_user['user_weight'] > $group['mg_weight']){
                            $_weight['apply'] = 2; // 超重
                            $_weight['user_name'] = $_user['user_real_name'];
                            break;
                        }
                    }
                    // 身高
                    if(!$_user['user_height']){
                        $_height['apply'] = 3; // 需要完善身高信息
                        $_height['user_name'] = $_user['user_real_name'];
                        break;
                    }else{
                        if($_user['user_height'] > $group['mg_height']){
                            $_height['apply'] = 2; // 超高
                            $_height['user_name'] = $_user['user_real_name'];
                            break;
                        }
                    }
                    // 性别
                    if($group['mg_gender']){
                        if($group['mg_gender'] != 3){
                            if($group['mg_gender'] != $_user['user_gender']){
                                $_gender['apply'] = 2;
                                $_gender['user_name'] = $_user['user_real_name'];
                                break;
                            }
                        }
                    }
                    // 年龄
                    if($group['mg_year']){
                        $user_age = $this->getAge($_user['user_birthday']);
                        if($user_age > $group['mg_year']){
                            $_age['apply'] = 2;
                            $_age['user_name'] = $_user['user_real_name'];
                            break;
                        }
                    }
                    // 船龄
                    if($group['mg_boat_year']){
                        $user_boat_age = $this->getAge($_user['user_sailing_time']);
                        if($user_boat_age > $group['mg_boat_year']){
                            $_boat_age['apply'] = 2;
                            $_boat_age['user_name'] = $_user['user_real_name'];
                            break;
                        }
                    }
                    */
                }
            }
    
            if($have['apply'] == 2){
                return json(['status' => 0, 'msg' => '用户' . $have['user_name'] . '已报名其他赛事']);
            }
            if($have_this['apply'] == 2){
                return json(['status' => 0, 'msg' => '用户' . $have['user_name'] . '已报名本场比赛']);
            }
            /*
            if($_weight['apply'] == 2){
                return json(['status' => 0, 'msg' => '用户' . $_weight['user_name'] . '体重超限制']);
            }
            if($_weight['apply'] == 3){
                return json(['status' => 0, 'msg' => '请完善用户' . $_weight['user_name'] . '体重信息']);
            }
            if($_height['apply'] == 2){
                return json(['status' => 0, 'msg' => '用户' . $_weight['user_name'] . '身高超限制']);
            }
            if($_height['apply'] == 3){
                return json(['status' => 0, 'msg' => '请完善用户' . $_weight['user_name'] . '身高信息']);
            }
            if($_gender['apply'] == 2){
                return json(['status' => 0, 'msg' => '用户' . $_weight['user_name'] . '性别不符']);
            }
            if($_age['apply'] == 2){
                return json(['status' => 0, 'msg' => '用户' . $_weight['user_name'] . '年龄超限制']);
            }
            if($_boat_age['apply'] = 2){
                return json(['status' => 0, 'msg' => '用户' . $_weight['user_name'] . '船龄超限制']);
            }
            */
            
            // 订单
            $order = [
                'order_user_id' => $this->user_id,
                'order_num' => $order_num,
                'order_price' => $ma_total_price,
                'order_time' => $this->create_time,
                'order_type' => $order_type,
                'order_match_id' => $match_id,
            ];
            if($order_type == 1 || $order_type == 2){ //支付
                $order['order_cate'] = 3;
                $order['order_nickname'] = $user['user_username'];
                $order['order_phone'] = $user['user_phone'];
            }else{ // 代付
                $order['order_cate'] = 4;
                if($order_type == 3){ // 微信代付
                    $qrcode = $this->WeChatPayReady($order_num, $ma_total_price);
                }else{ // 支付宝代付
                    $qrcode = $this->aliOtherPay($order_num, $ma_total_price);
                }
                if($qrcode['status'] == 200){
                    $other_qrcode = $this->otherPayQrcode($qrcode['msg'], $this->user_id, $order_num);
                    $order['order_another_qrcode'] = $other_qrcode;
                }else{
                    return json(['status' => 0, 'msg' => $qrcode['msg']]);
                }
            }
    
            Db::startTrans();
            try{
                Db::table('up_match_apply_info')->insert($apply);
                if(!empty($chuany)){
                    Db::table('up_match_apply_info')->insertAll($chuany);
                }
                $order_id = Db::table('up_order')->insertGetId($order);
                Db::commit();
                $order_info = Db::table('up_order')
                    ->field('order_id, order_another_qrcode')
                    ->where('order_id', $order_id)
                    ->find();
                return json(['status' => 200, 'msg' => $order_info]);
            }catch(\Exception $e){
                Db::rollback();
                return json(['status' => 0, 'msg' => '提交失败']);
            }
        }else{
            return json(['status' => 0, 'msg' => '请不要重复报名']);
        }
    }
    
    /**
     * 购买赛事照片
     * @return \think\response\Json
     * images    照片ID  逗号拼接字符串
     * total_price    总金额
     * order_type    支付方式   1 微信支付   2 支付宝支付   3 微信代付   4 支付宝代付
     */
    public function payImages(){
        $images_id = input('images'); // 照片ID,逗号拼接的字符串
        $total_price = input('total_price'); // 总金额
        $order_type = input('order_type'); // 支付方式 1 微信支付  2 支付宝支付  3 微信代付  4 支付宝代付
        $match_id = input('match_id');
        
        $user = Db::table('up_u_user')
            ->where('user_id', $this->user_id)
            ->field('user_urg_phone, user_sailing_time, user_username, user_phone')
            ->find();
        $order_num = date('YmdHis') . str_pad(mt_rand(1, 9999), 4, '0', STR_PAD_LEFT);
        $order = [
            'order_user_id' => $this->user_id,
            'order_num' => $order_num,
            'order_price' => $total_price,
            'order_time' => $this->create_time,
            'order_type' => $order_type,
            'order_image_id' => $images_id,
            'order_match_id' => $match_id
        ];
        if($order_type == 1 || $order_type == 2){ //支付
            $order['order_cate'] = 1;
            $order['order_nickname'] = $user['user_username'];
            $order['order_phone'] = $user['user_phone'];
        }else{ // 代付
            $order['order_cate'] = 2;
//            $order['order_nickname'] = $pay_user_name;
//            $order['order_phone'] = $pay_user_phone;
            if($order_type == 3){ // 微信代付
                $qrcode = $this->WeChatPayReady($order_num, $total_price);
            }else{ // 支付宝代付
                $qrcode = $this->aliOtherPay($order_num, $total_price);
            }
            if($qrcode['status'] == 200){
                $other_qrcode = $this->otherPayQrcode($qrcode['msg'], $this->user_id, $order_num);
                $order['order_another_qrcode'] = $other_qrcode;
            }else{
                return json(['status' => 0, 'msg' => $qrcode['msg']]);
            }
        }
        $res = Db::table('up_order')->insertGetId($order);
        if($res){
            $order_info = Db::table('up_order')
                ->field('order_id, order_another_qrcode')
                ->where('order_id', $res)
                ->find();
            return json(['status' => 200, 'msg' => $order_info]);
        }else{
            return json(['status' => 0, 'msg' => '提交失败']);
        }
    }
    
    /**
     * 报名详情
     * @return \think\response\Json
     * match_id      赛事ID
     */
    public function applyDetail(){
        $match_id = input('match_id');
        $apply = Db::table('up_match_apply_info')
            ->join('up_club', 'ma_club_id = club_id', 'left')
            ->join('up_match_group', 'ma_group = mg_id', 'left')
            ->join('up_match', 'match_id = ma_match_id', 'left')
            ->where('ma_user_id', $this->user_id)
            ->where('ma_match_id', $match_id)
            ->field('ma_user_name, ma_user_gender, ma_user_birthday, ma_user_ID_number, ma_user_call, 
            ma_user_email, ma_user_urg_phone, club_name, ma_lingd, ma_coach, ma_user_sailing_time, 
            mg_name, ma_fan_code, ma_boat_code, ma_total_price, ma_cost_info_id, ma_club_id, ma_group, 
            match_committee_phone, ma_user_fanlian_ID, ma_user_fanxie_ID')
            ->find();
        $apply['cost_detail'] = Db::table('up_match_cost_info')
            ->join('up_match_cost', 'mc_id = mi_cost_id', 'left')
            ->where('mi_id', 'IN', $apply['ma_cost_info_id'])
            ->field('mc_name, mi_price')
            ->select();
        $cost_arr = explode(',', $apply['ma_cost_info_id']);
        return json(['status' => 200, 'msg' => $apply, 'cost' => $cost_arr]);
    }
    
    /**
     * 报到&签到&上岸
     * @return \think\response\Json
     * match_id    赛事ID
     * type        1 报到   2 上岸
     */
    public function showQrcode(){
        $match_id = input('match_id');
        $type = input('type');
        if(isset($match_id) && $match_id != ''){ // 报到&上岸签到
            $match = Db::table('up_match_apply_info')
                ->join('up_match', 'match_id = ma_match_id', 'left')
                ->where('ma_user_id', $this->user_id)
                ->where('ma_match_id', $match_id)
                ->where('ma_apply_status', 2)
                ->field('ma_baodao_image, match_apply_addr, ma_role, ma_baodao_status, ma_up, ma_id, 
                match_apply_lon, match_apply_lat, ma_match_cate, match_status, ma_upwater_image')
                ->find();
            if(!$match){
                return json(['status' => 0, 'msg' => '没有相应的赛事']);
            }
            if($match['match_status'] == 3 || $match['match_status'] == 4){
                return json(['status' => 0, 'msg' => '比赛已结束或已取消']);
            }
            if($match['ma_role'] == 2){
                return json(['status' => 0, 'msg' => '您不是指定的报到人员']);
            }
            if($type == 1){
                if($match['ma_baodao_status'] == 2){
                    return json(['status' => 0, 'msg' => '请不要重复报到']);
                }
            }else{
                if($match['ma_up'] == 2){
                    return json(['status' => 0, 'msg' => '请不要重复上岸']);
                }
            }
            
            return json(['status' => 200, 'msg' => $match]);
        }else{
            $match = Db::table('up_match_apply_info')
                ->join('up_match', 'match_id = ma_match_id', 'left')
                ->where('ma_user_id', $this->user_id)
                ->where('ma_apply_status', 2)
//            ->where('ma_sgin_status', 1)
//            ->where('ma_baodao_status', 1)
                ->where('match_status', 'IN', '1,2')
                ->field('match_id, ma_role, ma_baodao_status, ma_sgin_status, ma_sgin_image, ma_baodao_image, 
            match_apply_addr, ma_id, match_apply_lon, match_apply_lat, ma_match_cate, match_start_time')
                ->order('match_start_time asc')
                ->limit(1)
                ->find();
            if(!$match){
                return json(['status' => 0, 'msg' => '您未报名任何赛事']);
            }
            if(strtotime($match['match_start_time']) > time()){
                return json(['status' => 0, 'msg' => '赛事未开始']);
            }
            if($match['ma_role'] == 2){
                return json(['status' => 0, 'msg' => '您不是指定的下水签到人员']);
            }
            if($match['ma_baodao_status'] == 1){
                return json(['status' => 0, 'msg' => '请先报到']);
            }
            if($match['ma_sgin_status'] == 2){
                return json(['status' => 100, 'msg' => '请不要重复签到','data'=>$match]);
            }
            if($match['ma_baodao_status'] == 1){
                $info['img'] = $match['ma_baodao_image'];
                $info['baodao_addr'] = $match['match_apply_addr'];
                $info['baodao_lon'] = $match['match_apply_lon'];
                $info['baodao_lat'] = $match['match_apply_lat'];
                $info['ma_match_cate'] = $match['ma_match_cate'];
            }else{
                $info['img'] = $match['ma_sgin_image'];
                $info['baodao_addr'] = '';
                $info['baodao_lon'] = '';
                $info['baodao_lat'] = '';
                $info['ma_match_cate'] = $match['ma_match_cate'];
            }
            return json(['status' => 200, 'msg' => $info]);
        }
        
    }

    /**
     * 扫描二维码
     * @return \think\response\Json
     * match_id    二维码中的赛事ID
     * other_user_id    二维码中的用户ID
     * type         二维码类型   1 报到   2 签到   3 上岸
     * sgin_addr    签到时的地点
     */
    public function saomiaoQrcode(){
        $match_id = input('match_id');
        $other_user_id = input('other_user_id');
        $type = input('type');
        $sgin_addr = input('sgin_addr');
        $role = Db::table('up_committee_member')
            ->where('cm_match_id', $match_id)
            ->where('cm_match_user_id', $this->user_id)
            ->field('cm_member_type')
            ->find();
        if(!$role){
            return json(['status' => 0, 'msg' => '没有您的相关比赛']);
        }
        if($role['cm_member_type'] != 7){
            return json(['status' => 0, 'msg' => '您不是本场比赛的竞赛检录人员']);
        }
        $apply = Db::table('up_match_apply_info')
            ->join('up_match', 'match_id = ma_match_id', 'left')
            ->join('up_club', 'ma_club_id = club_id', 'left')
            ->where('ma_match_id', $match_id)
            ->where('ma_user_id', $other_user_id)
            ->field('match_start_time, club_name, ma_user_name, ma_user_gender, ma_user_birthday, 
            ma_user_ID_number, ma_fan_code, ma_boat_code, ma_id, ma_sgin_status, ma_baodao_status, 
            ma_up, ma_user_fanlian_ID, ma_user_fanxie_ID, ma_sgin_date, ma_up_date, ma_upwater_image, 
            match_real_end_status')
            ->find();
        $today = date('Y-m-d');
        if($type == 1){ // 报到
            if($apply['ma_baodao_status'] == 2){
                return json(['status' => 0, 'msg' => '重复报到']);
            }
        }elseif($type == 2){ // 下水签到
            if($apply['ma_sgin_status'] == 2 || strtotime($today) == strtotime($apply['ma_sgin_date'])){
                return json(['status' => 0, 'msg' => '当日重复签到']);
            }
            // 自动签到成功
            $ma_upwater_image = $this->getQrcode($other_user_id, $match_id, 3);
            Db::startTrans();
            try{
                Db::table('up_match_apply_info')
                    ->where('ma_id', $apply['ma_id'])
                    ->update([
                        'ma_sgin_status' => 2,
                        'ma_sgin_time' => $this->create_time,
                        'ma_sgin_addr' => $sgin_addr,
                        'ma_hexiao_user_id' => $this->user_id,
                        'ma_down' => 2,
                        'ma_upwater_image' => $ma_upwater_image,
                        'ma_sgin_date' => date('Y-m-d')
                    ]);
                // ********************   北京同事代码  *****************//
                //将用户自动增加到比赛轮次相关设备组
                $match_detail_id = Db::table('up__match_detail')->where(['match_id'=>$match_id,'flag'=>0])->value('id');
                //获取船只颜色
                $color = randrgb();
                //将用户加入设备表
                $device_id = Db::table('up__device')->insertGetId(['type'=>5,'match_detail_id'=>$match_detail_id,'name'=>'船只','color'=>$color]);
                //将用户加入设备传感器关联表
                $res = Db::table('up__sensor_device')->insert(['sensor_id'=>'boat_'.$other_user_id,'device_id'=>$device_id,'match_detail_id'=>$match_detail_id,'name'=>$other_user_id]);
                // ********************   北京同事代码  *****************//
                Db::commit();
            }catch(\Exception $e){
                Db::rollback();
                return json(['status' => 0, 'msg' => '系统异常']);
            }
            $apply['ma_sgin_status'] = 2;
        }else{ // 上岸
            if($apply['ma_up'] == 2 || strtotime($today) == strtotime($apply['ma_up_date'])){
                return json(['status' => 0, 'msg' => '当日重复上岸签到']);
            }
            
            if(strtotime($apply['ma_sgin_date']) == strtotime($today)){ // 当日上岸签到
                $ma_up_date = date('Y-m-d');
            }else{ // 隔日上岸签到,上岸签到日期为下水签到日期，要不没法判断是否是当日重复上岸
                $ma_up_date = $apply['ma_sgin_date'];
            }
            
            Db::startTrans();
            try{
                // 自动上岸成功
                Db::table('up_match_apply_info')
                    ->where('ma_id', $apply['ma_id'])
                    ->update([
                        'ma_up' => 2,
                        'ma_up_time' => $this->create_time,
                        'ma_up_date' => $ma_up_date
                    ]);
                // todo：如果比赛没有结束，初始化队员当天的下水签到和上岸签到状态
                if($apply['match_real_end_status'] == 1){
                    Db::table('up_match_apply_info')
                        ->where('ma_id', $apply['ma_id'])
                        ->update([
                            'ma_sgin_status' => 1,
                            'ma_down' => 1,
                            'ma_up' => 1
                        ]);
                }
                Db::commit();
            }catch(\Exception $e){
                Db::rollback();
                return json(['status' => 0, 'msg' => '系统异常']);
            }
            $info = Db::table('up_match_apply_info')
                ->where('ma_match_id', $match_id)
                ->where('ma_user_id', $other_user_id)
                ->field('ma_up, ma_sgin_date, ma_up_date')
                ->find();
            if(strtotime($info['ma_sgin_date']) == strtotime($info['ma_up_date'])){
                $apply['ma_up'] = 2;
            }
        }
        
        return json(['status' => 200, 'msg' => $apply]);
    }
    
    /**
     * 确认报到&签到&上水信息
     * @return \think\response\Json
     * match_id    二维码中的赛事ID
     * other_user_id    二维码中的用户ID
     * type         二维码类型   1 报到   2 签到   3 上岸
     * sgin_addr    签到时的地点
     */
    public function commitSgin(){
        $match_id = input('match_id');
        $other_user_id = input('other_user_id');
        $type = input('type');
//        $sgin_addr = input('sgin_addr');
        $apply = Db::table('up_match_apply_info')
            ->where('ma_match_id', $match_id)
            ->where('ma_user_id', $other_user_id)
            ->field('ma_id')
            ->find();
        Db::startTrans();
        try{
            if($type == 1){ // 报到
                $ma_sgin_image = $this->getQrcode($other_user_id, $match_id, 2);
                // 报名信息
                Db::table('up_match_apply_info')
                    ->where('ma_id', $apply['ma_id'])
                    ->update([
                        'ma_baodao_status' => 2,
                        'ma_baodao_time' => $this->create_time,
                        'ma_sgin_image' => $ma_sgin_image
                    ]);
                // 比赛报到人数+1
                Db::table('up_match')->where('match_id', $match_id)->setInc('match_apply_num');
            }elseif($type == 2){
//                $ma_upwater_image = $this->getQrcode($other_user_id, $match_id, 3);
//                Db::table('up_match_apply_info')
//                    ->where('ma_id', $apply['ma_id'])
//                    ->update([
//                        'ma_sgin_status' => 2,
//                        'ma_sgin_time' => $this->create_time,
//                        'ma_sgin_addr' => $sgin_addr,
//                        'ma_hexiao_user_id' => $this->user_id,
//                        'ma_down' => 2,
//                        'ma_upwater_image' => $ma_upwater_image
//                    ]);
            }else{
//                Db::table('up_match_apply_info')
//                    ->where('ma_id', $apply['ma_id'])
//                    ->update([
//                        'ma_up' => 2,
//                        'ma_up_time' => $this->create_time
//                    ]);
            }
            
            Db::commit();
            return json(['status' => 200, 'msg' => '核销成功']);
        }catch(\Exception $e){
            return json(['status' => 0, 'msg' => '系统异常，核销失败']);
        }
    }
    
    /**
     * 上岸签到---备用
     * @return \think\response\Json
     * match_id   赛事ID
     */
    public function upSgin1(){
        $match_id = input('match_id');
        $res = Db::table('up_match_apply_info')
            ->where('ma_match_id', $match_id)
            ->where('ma_user_id', $this->user_id)
            ->update([
                'ma_up' => 2,
                'ma_up_time' => $this->create_time
            ]);
        if($res){
            return json(['status' => 200, 'msg' => true]);
        }else{
            return json(['status' => 0, 'msg' => false]);
        }
    }
    
    /**
     * 判断用户有没有扫码权限
     * @return \think\response\Json
     */
    public function haveRole(){
        $role = Db::table('up_committee_member')
            ->join('up_match', 'match_id = cm_match_id', 'left')
            ->where('cm_match_user_id', $this->user_id)
            ->where('match_status', 'IN', '1,2')
            ->where('cm_member_type', 7)
            ->find();
        if($role){
            return json(['status' => 200, 'msg' => true]);
        }else{
            return json(['status' => 0, 'msg' => '您不是指定的竞赛检录人员']);
        }
    }
    
    /**
     * 扫描签到二维码------备用
     * @return \think\response\Json
     * match_id    二维码中的赛事ID
     * other_user_id    二维码中的用户ID
     */
    public function smSginQrcode(){
        $match_id = input('match_id');
        $other_user_id = input('other_user_id');
        $sgin_addr = input('sgin_addr');
        $role = Db::table('up_committee_member')
            ->where('cm_match_id', $match_id)
            ->where('cm_match_user_id', $this->user_id)
            ->field('cm_member_type')
            ->find();
        if(!$role){
            return json(['status' => 0, 'msg' => '您不是本场比赛的竞赛检录人员']);
        }
        if($role['cm_member_type'] != 7){
            return json(['status' => 0, 'msg' => '您不是本场比赛的竞赛检录人员']);
        }
        $sgin = Db::table('up_match_apply_info')
            ->join('up_match', 'match_id = ma_match_id', 'left')
            ->join('up_club', 'ma_club_id = club_id', 'left')
            ->where('ma_match_id', $match_id)
            ->where('ma_user_id', $other_user_id)
            ->field('match_start_time, club_name, ma_user_name, ma_user_gender, ma_user_birthday, 
            ma_user_ID_number, ma_fan_code, ma_boat_code, ma_id, ma_sgin_status, ma_user_fanlian_ID, 
            ma_user_fanxie_ID')
            ->find();
    
        if(time() > strtotime($sgin['match_start_time'])){
            return json(['status' => 0, 'msg' => '比赛已经开始']);
        }
        if($sgin['ma_sgin_status'] == 2){
            return json(['status' => 200, 'msg' => '请不要重复签到']);
        }
    
        $res = Db::table('up_match_apply_info')
            ->where('ma_id', $sgin['ma_id'])
            ->update([
                'ma_sgin_status' => 2,
                'ma_sgin_time' => $this->create_time,
                'ma_sgin_addr' => $sgin_addr,
                'ma_hexiao_user_id' => $this->user_id
            ]);
        if($res){
            return json(['status' => 200, 'msg' => $sgin]);
        }else{
            return json(['status' => 0, 'msg' => '系统异常，签到失败']);
        }
    }
    
    /**
     * 船长上岸状态---原逻辑
     * @return \think\response\Json
     */
    public function isUp1(){
        $match = Db::table('up_match_apply_info')
            ->join('up_match', 'match_id = ma_match_id', 'left')
            ->where('ma_user_id', $this->user_id)
            ->where('ma_apply_status', 2)
//            ->where('ma_down', 2)
//            ->where('match_status', 2)
            ->field('ma_up, match_id, ma_sgin_status, ma_match_cate, ma_sgin_date, ma_up_date')
            ->order('match_start_time asc')
            ->limit(1)
            ->find();
        if(!empty($match['ma_sgin_date']) && !empty($match['ma_up_date']) && (strtotime($match['ma_sgin_date']) == strtotime($match['ma_up_date']))){
            $match['ma_up'] = 2;
        }
        if($match){
            return json(['status' => 200, 'msg' => $match]);
        }else{
            return json(['status' => 100, 'msg' => '没有相关赛事']);
        }
    }
    
    /**
     * 船长上岸状态---现逻辑
     * @return \think\response\Json
     */
    public function isUp(){
        $match = Db::table('up_match_apply_info')
            ->where('ma_user_id', $this->user_id)
            ->where('ma_apply_status', 2)
            ->field('ma_up, ma_match_id, ma_sgin_status, ma_match_cate, ma_sgin_date, ma_up_date')
            ->select();
        $status = []; // ma_up == 1  未上岸   ma_up == 2   已上岸
        $is_up = 2; // 如果有未上岸的记录，则置1
        if($match){
            foreach($match as $k => $v){
                if(!empty($v['ma_sgin_date']) && empty($v['ma_up_date'])){ // 第一天没上岸
                    $is_up = 1;
                    $status['match_id'] = $v['ma_match_id'];
                    break;
//                    $status['ma_up'] = 1;
                }elseif(empty($v['ma_sgin_date']) && empty($v['ma_up_date'])){ // 第一天没下水
                    $status['match_id'] = $v['ma_match_id'];
                    continue;
//                    $status['ma_up'] = 2;
                }else{
                    if(strtotime($v['ma_sgin_date']) == strtotime($v['ma_up_date'])){
                        $status['match_id'] = $v['ma_match_id'];
                        continue;
//                        $status['ma_up'] = 2;
                    }else{
                        $is_up = 1;
                        $status['match_id'] = $v['ma_match_id'];
                        break;
//                        $status['ma_up'] = 1;
                    }
                }
            }
            if($is_up == 1){
                $status['ma_up'] = 1;
            }else{
                $status['ma_up'] = 2;
            }
            $match_cate = $match[0]['ma_match_cate'];
            return json(['status' => 200, 'msg' => $status, 'match' => $match, 'match_cate' => $match_cate]);
        }else{
            return json(['status' => 100, 'msg' => '没有相关赛事']);
        }
    }
    
    /**
     * 是否下水签到
     * @return \think\response\Json
     */
    public function isSgin(){
        $match = Db::table('up_match_apply_info')
            ->join('up_match', 'match_id = ma_match_id', 'left')
            ->where('ma_user_id', $this->user_id)
            ->where('ma_apply_status', 2)
//            ->where('ma_down', 2)
            ->where('match_status', 2)
            ->field('match_id, ma_sgin_status, ma_match_cate')
            ->order('match_start_time asc')
            ->limit(1)
            ->find();
        if($match){
            return json(['status' => 200, 'msg' => $match]);
        }else{
            return json(['status' => 100, 'msg' => '没有相关赛事']);
        }
    }
    
    /**
     * 用户是否有竞赛官权限
     * @return \think\response\Json
     * match_id    赛事ID
     */
    public function isZong(){
        $match_id = input('match_id');
        if(isset($match_id) && $match_id != ''){ // 赛事详情报到
            $role = Db::table('up_committee_member')
                ->where('cm_match_id', $match_id)
                ->where('cm_match_user_id', $this->user_id)
                ->where('cm_member_type', 2)
                ->field('cm_match_id, cm_member_role')
                ->find();
        }else{
            $role = Db::table('up_committee_member')
                ->join('up_match', 'match_id = cm_match_id', 'left')
                ->where('cm_match_user_id', $this->user_id)
                ->where('match_status', 'IN', '1,2')
                ->where('cm_member_type', 2)
                ->field('cm_match_id, cm_member_role')
                ->find();
        }
        if($role){
            return json(['status' => 200, 'msg' => $role]);
        }else{
            return json(['status' => 0, 'msg' => '您不是该场次比赛的总裁判']);
        }
    }
    
    /**
     * 信号旗
     * @return \think\response\Json
     */
    public function xinhao(){
        $list = Db::table('up_flag_signal')
            ->where('fs_status', 1)
            ->field('fs_id, fs_name, fs_image')
            ->select();
        if($list){
            return json(['status' => 200, 'msg' => $list]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 航线图
     * @return \think\response\Json
     */
    public function sailImage(){
        $list = Db::table('up_match_sail_image')
            ->where('msi_status', 1)
            ->field('msi_id, msi_name, msi_image, msi_num, msi_raobiao, msi_biao_name')
            ->select();
        if($list){
            return json(['status' => 200, 'msg' => $list]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 起航旗
     * @return \think\response\Json
     */
    public function startFlag(){
        $list = Db::table('up_setsail_flag')
            ->where('sf_status', 1)
            ->field('sf_id, sf_name, sf_image')
            ->select();
        if($list){
            return json(['status' => 200, 'msg' => $list]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    //******************************************  赛艇报名
    /**
     * 赛艇报名
     * @return \think\response\Json
     */
    public function commitRowing(){
        $params = input('apply');
        $params = json_decode($params, true);
        
        $match_id = $params['match_id']; // 赛事ID
        $club_id = $params['club_id']; // 俱乐部ID
        $ma_lingd = $params['ma_lingd']; // 领队
        $ma_all_price = $params['ma_all_price']; // 本次报名的总费用---订单表
        $dui = $params['dui']; // 队
        
        //**********************************订单
        $order_type = $params['order_type']; // 订单类型   1 微信支付   2 支付宝支付   3 微信代付   4 支付宝代付
        $order_num = date('YmdHis') . str_pad(mt_rand(1, 9999), 4, '0', STR_PAD_LEFT);
    
        $match = Db::table('up_match')
            ->where('match_id', $match_id)
            ->field('match_cate, match_start_time, match_apply_end_time')
            ->find();
    
        $user = Db::table('up_u_user')
            ->where('user_id', $this->user_id)
            ->field('user_username, user_phone')
            ->find();
    
        if(time() > strtotime($match['match_apply_end_time'])){
            return json(['status' => 0, 'msg' => '报名已结束']);
        }
        
        $insertAll = []; // 最终的数组
        $tag = [
            'rep_user' => 1, // 报名人是否重复---备用
            'rep_teamname' => 1, // 队名是否重复
        ];
        $group_man = ['is_man' => 1, 'group_name' => '']; // 判断某个组是否报满
        $group_arr = []; // 组别是否已报满
        $user_arr = []; // 报名队员赛事冲突
        $team_arr = []; // 队名是否重复
        
        if(is_array($dui) && $dui != ''){
//            $dui_num = count($dui);
            foreach ($dui as $k => $v) {
                
                // 队名
                if(in_array($v['ma_team_name'], $team_arr)){
                    $tag['rep_teamname'] = 2;
                }else{
                    array_push($team_arr, $v['ma_team_name']);
                }
                // 组
                if(array_key_exists($v['ma_group'], $group_arr)){
                    $group_arr[$v['ma_group']] += 1;
                }else{
                    $group_arr[$v['ma_group']] = 1;
                }
                
                if(is_array($v['team_user']) && $v['team_user'] != ''){
                    foreach ($v['team_user'] as $kk => $vv) {
                        
                        // 报名人员
                        if(in_array($vv['ma_user_id'], $user_arr)){
                            $tag['rep_user'] = 2;
                        }else{
                            array_push($user_arr, $vv['ma_user_id']);
                        }
                        
                        $insertAll[$k.$kk] = [
                            'ma_user_id' => $vv['ma_user_id'],
                            'ma_match_id' => $match_id,
                            'ma_total_price' => $v['ma_total_price'],
                            'ma_cost_info_id' => trim($v['ma_cost_info_id'], ','),
                            'ma_club_id' => $club_id,
                            'ma_lingd' => $ma_lingd,
                            'ma_group' => $v['ma_group'],
                            'ma_team_name' => $v['ma_team_name'],
                            'ma_boat_type' => $v['ma_boat_type'],
                            'ma_seat' => $vv['ma_seat'],
                            'ma_distance' => $v['ma_distance'],
                            'ma_real_pay_user_id' => $this->user_id,
                            'ma_order_num' => $order_num,
                            'ma_time' => $this->create_time,
                            'ma_date' => date('Y-m-d'),
                            'ma_match_cate' => $match['match_cate'],
                            'ma_user_name' => $vv['ma_user_name'],
                            'ma_user_gender' => $vv['ma_user_gender'],
                            'ma_user_birthday' => $vv['ma_user_birthday'],
                            'ma_user_ID_number' => $vv['ma_user_ID_number'],
                            'ma_user_call' => $vv['ma_user_call'],
                            'ma_user_email' => $vv['ma_user_email'],
                            'ma_user_urg_phone' => $vv['ma_user_urg_phone'],
                            'ma_user_sailing_time' => $vv['ma_user_sailing_time']
                        ];
                        // 签到人员&二维码
                        if($v['ma_role'] == $vv['ma_user_id']){
                            $insertAll[$k.$kk]['ma_role'] = 1;
                            $insertAll[$k.$kk]['ma_baodao_image'] = $this->getQrcode($vv['ma_user_id'], $match_id, 1);
                        }else{
                            $insertAll[$k.$kk]['ma_role'] = 2;
                            $insertAll[$k.$kk]['ma_baodao_image'] = '';
                        }
                    }
                }
            }
            
            // ************* 队名有没有重复
            $have_team_name = Db::table('up_match_apply_info')
                ->where('ma_team_name', 'IN', $team_arr)
                ->where('ma_match_id', $match_id)
                ->column('ma_team_name');
            if($have_team_name){
                $same_name = array_intersect($have_team_name, $team_arr);
                $same = implode(',', $same_name);
                return json(['status' => 0, 'msg' => $same . '重复']);
            }
            if($tag['rep_teamname'] == 2){
                return json(['status' => 0, 'msg' => '队名不可以重复']);
            }
    
            // *************** 报名重复----赛事冲突&已报名本场比赛
            if($tag['rep_user'] == 2){
                return json(['status' => 0, 'msg' => '一个队员只能参加一个组']);
            }
            // 本场比赛
            $this_sgin = Db::table('up_match_apply_info')
                ->where('ma_user_id', 'IN', $user_arr)
                ->where('ma_match_id', $match_id)
                ->where('ma_apply_status', 2)
                ->column('ma_user_id');
            if($this_sgin){
                $same_user_id = array_intersect($this_sgin, $user_arr);
                $same_user_name = Db::table('up_match_apply_info')
                    ->where('ma_user_id', 'IN', $same_user_id)
                    ->column('ma_user_name');
                $same_user = implode(',', $same_user_name);
                return json(['status' => 0, 'msg' => $same_user . '已经参加了本场比赛']);
            }
            // 其他冲突的比赛
            $otherMatch = Db::table('up_match_apply_info')
                ->join('up_match', 'match_id = ma_match_id', 'left')
                ->where('ma_user_id', 'IN', $user_arr)
//                ->where('match_end_time', '>', $match['match_start_time'])
                ->where('match_status', 'IN', '1,2')
                ->where('match_id', 'NEQ', $match_id)
                ->where('ma_apply_status', 2)
                ->column('ma_user_id');
            if($otherMatch){
                $same_match_user_id = array_intersect($otherMatch, $user_arr);
                $same_match_user_name = Db::table('up_match_apply_info')
                    ->where('ma_user_id', 'IN', $same_match_user_id)
                    ->column('ma_user_name');
                $same_user = implode(',', array_unique($same_match_user_name));
                return json(['status' => 0, 'msg' => $same_user . '已参加了其他有冲突赛事']);
            }
            
            // ***************** 报名组别有没有报满
            if(!empty($group_arr)){
                // $k:组ID   $v:报名的数量
                foreach($group_arr as $k => $v){
                    $group_info = Db::table('up_match_apply_group')
                        ->join('up_match_group', 'mag_group_id = mg_id', 'left')
                        ->where('mag_match_id', $match_id)
                        ->where('mag_group_id', $k)
                        ->field('mag_apply_num, mag_already_num, mg_name')
                        ->find();
                    $person_num = $v + $group_info['mag_already_num'];
                    if($person_num > $group_info['mag_apply_num']){
                        $group_man['is_man'] = 2;
                        $group_man['group_name'] = $group_info;
                    }
                }
            }
            if($group_man['is_man'] == 2){
                return json(['status' => 0, 'msg' => $group_man['group_name'] . '报名人数超限']);
            }
        }
    
        // 订单
        $order = [
            'order_user_id' => $this->user_id,
            'order_num' => $order_num,
            'order_price' => $ma_all_price,
            'order_time' => $this->create_time,
            'order_type' => $order_type,
            'order_match_id' => $match_id,
        ];
        
        if($order_type == 1 || $order_type == 2){ //支付
            $order['order_cate'] = 3;
            $order['order_nickname'] = $user['user_username'];
            $order['order_phone'] = $user['user_phone'];
        }else{ // 代付
            $order['order_cate'] = 4;
            if($order_type == 3){ // 微信代付
                $qrcode = $this->WeChatPayReady($order_num, $ma_all_price);
            }else{ // 支付宝代付
                $qrcode = $this->aliOtherPay($order_num, $ma_all_price);
            }
            if($qrcode['status'] == 200){
                $other_qrcode = $this->otherPayQrcode($qrcode['msg'], $this->user_id, $order_num);
                $order['order_another_qrcode'] = $other_qrcode;
            }else{
                return json(['status' => 0, 'msg' => $qrcode['msg']]);
            }
        }
        
        Db::startTrans();
        try{
            if(!empty($insertAll)){
                Db::table('up_match_apply_info')->insertAll($insertAll);
            }
            $order_id = Db::table('up_order')->insertGetId($order);
            Db::commit();
            $order_info = Db::table('up_order')
                ->field('order_id, order_another_qrcode')
                ->where('order_id', $order_id)
                ->find();
            return json(['status' => 200, 'msg' => $order_info]);
        }catch(\Exception $e){
            Db::rollback();
            return json(['status' => 0, 'msg' => $e->getMessage()]);
        }
    }
    
    /**
     * 赛艇报名详情
     * @return \think\response\Json
     * match_id    赛事ID
     */
    public function rowingApplyDetail(){
        $match_id = input('match_id');
        $match = Db::table('up_match')
            ->where('match_id', $match_id)
            ->field('match_stop_time')
            ->find();
        if(strtotime($match['match_stop_time']) >= time()){
            $can = 1; // 可以编辑
        }else{
            $can = 2; // 不可以编辑
        }
        $one_apply = Db::table('up_match_apply_info')
            ->join('up_match', 'match_id = ma_match_id', 'left')
            ->join('up_club', 'club_id = ma_club_id', 'left')
            ->field('ma_order_num, club_name, club_image, ma_club_id, ma_lingd, match_committee_phone')
            ->where('ma_real_pay_user_id', $this->user_id)
            ->where('ma_match_id', $match_id)
            ->find();
        $team = Db::table('up_match_apply_info')
            ->join('up_match_group', 'mg_id = ma_group', 'left')
            ->where('ma_order_num', $one_apply['ma_order_num'])
            ->field('mg_name, ma_group, ma_team_name, ma_boat_type, ma_distance, ma_total_price, 
            ma_cost_info_id')
            ->group('ma_team_name')
            ->select();
        $total_price = 0; // 总费用
        foreach($team as $k => $v){
            // 队员
            $user = Db::table('up_match_apply_info')
                ->join('up_u_user', 'ma_user_id = user_id', 'left')
                ->where('ma_team_name', $v['ma_team_name'])
                ->field('user_phone, ma_seat, ma_user_id, ma_id, ma_role')
                ->select();
            $team[$k]['user'] = $user;
            // 费用明细
            $price = Db::table('up_match_cost_info')
                ->join('up_match_cost', 'mi_cost_id = mc_id', 'left')
                ->where('mi_id', 'IN', $v['ma_cost_info_id'])
                ->field('mc_name, mi_price')
                ->select();
            $team[$k]['price_info'] = $price;
            // 总费用
            $total_price += $v['ma_total_price'];
        }
        $one_apply['all_price'] = $total_price;
        $one_apply['team'] = $team;
        return json(['status' => 200, 'msg' => $one_apply, 'can' => $can]);
    }
    
    /**
     * 编辑船员时，展示原船员的信息
     * @return \think\response\Json
     * ma_id     报名信息ID
     */
    public function updateRowingUser(){
        $ma_id = input('ma_id');
        $user = Db::table('up_match_apply_info')
            ->join('up_u_user', 'user_id = ma_user_id', 'left')
            ->field('user_phone, ma_role, ma_user_name, ma_user_gender, ma_user_birthday, ma_user_ID_number, 
            ma_user_call, ma_user_email, ma_user_urg_phone, ma_user_sailing_time')
            ->where('ma_id', $ma_id)
            ->find();
        return json(['status' => 200, 'msg' => $user]);
    }
    
    /**
     * 编辑船员时，根据手机号查找用户
     * @return \think\response\Json
     * match_id    赛事ID
     * user_phone   手机号
     * ma_id        报名记录ID
     */
    public function updateCheck(){
        $match_id = input('match_id');
        $user_phone = input('user_phone');
        $ma_id = input('ma_id');
        $user = Db::table('up_u_user')
            ->where('user_phone', $user_phone)
            ->field('user_id')
            ->find();
        $match = Db::table('up_match')
            ->where('match_id', $match_id)
            ->field('match_start_time')
            ->find();
        $apply = Db::table('up_match_apply_info')
            ->where('ma_id', $ma_id)
            ->field('ma_team_name')
            ->find();
        if(!$user){
            return json(['status' => 0, 'msg' => '用户不存在']);
        }
        // 是否已在本场赛事的本队中
        $isApply = Db::table('up_match_apply_info')
            ->where('ma_user_id', $user['user_id'])
            ->where('ma_match_id', $match_id)
            ->where('ma_id', 'NEQ', $ma_id)
            ->where('ma_team_name', $apply['ma_team_name'])
            ->find();
        if($isApply){
            return json(['status' => 0, 'msg' => '该船员已在本报名队中']);
        }
        // 和其他赛事冲突
        $otherMatch = Db::table('up_match_apply_info')
            ->join('up_match', 'match_id = ma_match_id', 'left')
            ->where('ma_user_id', $user['user_id'])
//            ->where('match_end_time', '>', $match['match_start_time'])
            ->where('match_status', 'IN', '1,2')
            ->where('match_id', 'NEQ', $match_id)
            ->where('ma_apply_status', 2)
            ->field('match_id')
            ->find();
        if($otherMatch) return json(['status' => 0, 'msg' => '该赛事与该队员报名的其他赛事有冲突']);
        // 已报名本场比赛
        $thisMatch = Db::table('up_match_apply_info')
            ->where('ma_user_id', $user['user_id'])
            ->where('ma_match_id', $match_id)
            ->where('ma_id', 'NEQ', $ma_id)
            ->where('ma_apply_status', 2)
            ->find();
        if($thisMatch) return json(['status' => 0, 'msg' => '该船员已参加本场赛事']);
        return json(['status' => 200, 'msg' => $user['user_id']]);
    }
    
    /**
     * 赛艇编辑船员确定时
     * @return \think\response\Json
     * ma_id     报名记录ID
     * ma_user_id    队员ID
     * ma_user_name   船员姓名
     * ma_user_gender   性别   1 男   2 女
     * ma_user_birthday    生日
     * ma_user_ID_number   身份证号
     * ma_user_call        电话
     * ma_user_email       邮箱
     * ma_user_urg_phone   紧急联系人手机号
     * ma_user_sailing_time   航海时间
     */
    public function updateApply(){
        $ma_id = input('ma_id');
        $ma_user_id = input('ma_user_id');
        $ma_user_name = input('ma_user_name');
        $ma_user_gender = input('ma_user_gender');
        $ma_user_birthday = input('ma_user_birthday');
        $ma_user_ID_number = input('ma_user_ID_number');
        $ma_user_call = input('ma_user_call');
        $ma_user_email = input('ma_user_email');
        $ma_user_urg_phone = input('ma_user_urg_phone');
        $ma_user_sailing_time = input('ma_user_sailing_time');
        
        $update = [
            'ma_user_id' => $ma_user_id,
            'ma_user_name' => $ma_user_name,
            'ma_user_gender' => $ma_user_gender,
            'ma_user_birthday' => $ma_user_birthday,
            'ma_user_ID_number' => $ma_user_ID_number,
            'ma_user_call' => $ma_user_call,
            'ma_user_email' => $ma_user_email,
            'ma_user_urg_phone' => $ma_user_urg_phone,
            'ma_user_sailing_time' => $ma_user_sailing_time,
            'ma_update_time' => $this->create_time
        ];

        $res = Db::table('up_match_apply_info')->where('ma_id', $ma_id)->update($update);
        if($res){
            return json(['status' => 200, 'msg' => '编辑成功']);
        }else{
            return json(['status' => 0, 'msg' => '编辑失败']);
        }
    }
    
    /**
     * 默认俱乐部/实名认证俱乐部
     * @return \think\response\Json
     */
    public function defaultClub(){
        $club = Db::table('up_u_user')
            ->join('up_club', 'club_id = user_club_id', 'left')
            ->field('club_id, club_name')
            ->where('user_id', $this->user_id)
            ->find();
        return json(['status' => 200, 'msg' => $club]);
    }
    
    /**
     * 返回订单支付状态
     * @param $order_id    订单ID
     * @return \think\response\Json
     */
    public function orderStatus($order_id){
        $status = Db::table('up_order')
            ->where('order_id', $order_id)
            ->field('order_status')
            ->find();
        return json(['status' => 200, 'msg' => $status['order_status']]);
    }
    
    /**
     * 官方公告
     * @param $type     调用类型     1 列表     2 统计未读
     * @param int $match_id         赛事ID    从赛事页面进入需要传
     * @return $this|false|int|\PDOStatement|string|\think\Collection
     */
    private function gf($type, $match_id = 0){
        $where = [];
        if(isset($match_id) && $match_id != 0){
            $where['mgf_match_id'] = $match_id;
        }
        if($type == 2){ // 未读
            $where['mgf_read_user'] = ['notlike', '%,' . $this->user_id . ',%'];
            $where['mgf_del_user'] = ['notlike', '%,' . $this->user_id . ',%'];
        }
        $data = Db::table('up_match_gf')
            ->where($where);
        if($type == 1){ // 列表
            $data = $data->field('mgf_id, mgf_match_id, mgf_title, mgf_intro, mgf_read_user, mgf_del_user, mgf_time')
                ->order('mgf_time desc')
                ->page($this->page, 10)
                ->select();
        }else{
            $data = $data->count();
        }
        return $data;
    }
    
    /**
     * 临时通知
     * @param $type     调用类型     1 列表     2 统计未读
     * @param int $match_id         赛事ID    从赛事页面进入需要传
     * @return $this|false|int|\PDOStatement|string|\think\Collection
     */
    private function ls($type, $match_id = 0){
        $where = [];
        if(isset($match_id) && $match_id != 0){
            $where['mfls_match_id'] = $match_id;
        }
        if($type == 2){ // 未读
            $where['mfls_read_user'] = ['notlike', '%,' . $this->user_id . ',%'];
            $where['mfls_del_user'] = ['notlike', '%,' . $this->user_id . ',%'];
        }
        $data = Db::table('up_match_ls')
            ->where($where);
        if($type == 1){ // 列表
            $data = $data->field('mfls_id, mfls_match_id, mfls_title, mfls_match_intro, mfls_read_user,
             mfls_del_user, mfls_time')
                ->order('mfls_time desc')
                ->page($this->page, 10)
                ->select();
        }else{
            $data = $data->count();
        }
        return $data;
    }
}